#include "../include/Configuration.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace wd;
using std::ifstream;
using std::istringstream;
using std::cerr;

Configuration::Configuration(const string& filepath)
: _filepath(filepath)
{
    readConf();
}

Configuration * Configuration::getpInstance()
{
    /* if(!_pInstance){ */
    /*     _pInstance = new Configuration("/home/hcg/CPP2/SE_v2/conf/setting.conf"); */
    /* } */
    return _pInstance;
}

void Configuration::destroy()
{
    if(_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}

Configuration::~Configuration()
{
    destroy();
}

map<string,string>& Configuration::getConfigMap()
{
    return _configMap;
}

void Configuration::readConf()
{
    ifstream ifs(_filepath);
    ifs.seekg(0);
    if(!ifs.good()){
        cerr << ">> ifstream open file error!\n";
        return;
    }
    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key,value;
        iss >> key >> value;
        _configMap[key] = value;
    }
    ifs.close();
}

/* Configuration * Configuration::_pInstance = nullptr; */
#if 0
void test(){
    Configuration * conf = Configuration::getpInstance();
    auto configMap = conf->getConfigMap();
    for(auto& elem : configMap){
        std::cout << elem.first << " " << elem.second << std::endl;
    }
}

int main(){
    test();
    return 0;
}
#endif
